package com.neuedu.carsystem.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.carsystem.model.Employee;
import com.neuedu.carsystem.model.Job;
import com.neuedu.carsystem.service.EmployeeService;

@Controller
@RequestMapping("/findEmployee")
public class EmployeeAction {
	@Autowired
	private EmployeeService employeeService;

	// 分页查询所有
	@RequestMapping("/findEmployeeAll")
	public String findEmployeeAll(HttpServletRequest request, HttpServletResponse response) {
		int page;
		if (request.getParameter("page")==null) {
			page=1;
		}else {
			page = Integer.parseInt(request.getParameter("page"));
		}
		
//		int page = Integer.parseInt(request.getParameter("page"));
		if (request.getParameter("page") == null) {
			page = 1;
		}
		int next = page + 1;
		int pre = page - 1;
		int pageSize = 4;
		Map<String, Object> pageMap = new HashMap<String, Object>();
		// 从第几条数据开始查询
		int total = employeeService.countEmployee();

		int pageCount = (int) Math.ceil(1.0 * total / pageSize);
		if (page < 1) {
			page = 1;
		}

		pageMap.put("startPage", (page - 1) * pageSize);
		// 每页查询多少条记录
		pageMap.put("endPage", pageSize * page);
		// 进行分页查询
		List<Employee> employeelist = employeeService.findEmployeeAll(pageMap);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("employeeList", employeelist);
		request.setAttribute("total", total);
		request.setAttribute("nowpage", page);
		request.setAttribute("prepage", pre);
		request.setAttribute("nextpage", next);
		System.out.println(next);
		return "employee_list";
		// return "redirect:/carlist.jsp";
	}

	// 添加
	@RequestMapping("/addEmployee")
	@ResponseBody
	public String addEmployee(HttpServletRequest request, HttpServletResponse response) {
		String ename = request.getParameter("ename");
		int age = Integer.parseInt(request.getParameter("age"));
		String sex = request.getParameter("sex");
		String hireDate = request.getParameter("hiredate");
		System.out.println(ename + "," + age + "," + sex + "," + sex + "," + hireDate);
		// 将字符串转换为时间格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date hiredate = null;
		try {
			hiredate = sdf.parse(hireDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int esal = Integer.parseInt(request.getParameter("esal"));
		System.out.println(ename + "," + age + "," + sex + "," + esal + ",");
		Employee employee = new Employee(0, ename, age, sex, hiredate, esal, null);
		try {
			int ret = employeeService.addEmployee(employee);
		} catch (Exception e) {
		}
		// return "{success:true}";
		return "redirect:/findEmployee/findEmployeeAll.do";
	}

	// 修改
	@RequestMapping("/updateEmployee")
	public String updateJob(HttpServletRequest request, HttpServletResponse response) {
		int empno = Integer.parseInt(request.getParameter("empno"));
		System.out.println("修改id为：" + empno);
		String ename = request.getParameter("ename");
		int age = Integer.parseInt(request.getParameter("age"));
		String sex = request.getParameter("sex");
		String hireDate = request.getParameter("hireDate");
		// 将字符串转换为时间格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date hiredate = null;
		try {
			hiredate = sdf.parse(hireDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int esal = Integer.parseInt(request.getParameter("esal"));
		int jno = Integer.parseInt(request.getParameter("jno"));
		Job job = new Job();
		job.setJno(jno);
		System.out.println(ename + "," + age + "," + sex + "," + esal + "," + jno);
		// int ret = carService.updateCar(carno);
//			
		return null;
	}

	// 删除
	@RequestMapping("/deleteEmployee")
	public String deleteJob(Integer id, HttpServletRequest request, HttpServletResponse response) {
		int ret = employeeService.deleteEmployee(id);
		return null;
	}

}
